<?php

$myts =& MyTextSanitizer::getInstance() ;

require_once dirname(dirname(__FILE__)).'/include/upload_functions.php' ;

$url = isset( $_GET['url'] ) ? htmlspecialchars( $_GET['url'], ENT_QUOTES ) : "";
$filename = isset( $_GET['filename'] ) ? htmlspecialchars( $_GET['filename'], ENT_QUOTES ) : "";
$ext = isset( $_GET['ext'] ) ? htmlspecialchars( $_GET['ext'], ENT_QUOTES ) : "";

if ( ! preg_match("/^http:\/\//", $url) ) {
	@set_time_limit(0);
	if( empty( $filename ) ){
		$f_info = pathinfo( $url );
		$filename = $f_info['basename'];
		$ext = strtolower( $f_info['extension'] ) ;
	}
	$mtype_arr = d3download_return_mtype() ;
	$mtype = $mtype_arr[$ext];
	$size= @filesize( $url );
	if( headers_sent() ) die( 'headers are already sent' ) ;
	if ( ! empty( $mtype ) ) {
		header('Content-Type: '.$mtype.'');
	} else {
		header('Content-Type: application/force-download');
	}
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header('Content-Description: File Transfer');
	header('Content-Length: '.$size.'' );

	if ( $size > 1024*1024*2 ) {
		ob_clean();
		ini_set('memory_limit',"12M");
		$fp=fopen( $url, 'r' );
		while(!feof($fp)) {
			$buffer = fread( $fp, 1024*6 ); 
			echo $buffer;
			flush();
			ob_flush();
			usleep(2000); 
		}
		fclose($fp);
	} else {
		@readfile($url);
	}
	exit();
} else {
	Header("Location: $url");
	echo "<html><head><meta http-equiv=\"Refresh\" content=\"0; URL=".$myts->oopsHtmlSpecialChars($url)."\"></meta></head><body></body></html>";
	exit();
}

?>